package com.mud.dnd.frontend.ws;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.jws.WebService;

import com.mud.dnd.domain.common.DAOException;
import com.mud.dnd.domain.common.QueryParameter;
import com.mud.dnd.domain.common.QueryParameter.QueryParamOperator;
import com.mud.dnd.domain.model.Card;
import com.mud.dnd.ejb.CardManager;
import com.mud.dnd.ejb.common.AuthenticationException;
import com.mud.dnd.frontend.ws.common.RestInputConverter;;

@WebService
public class CardJSONService 
{

	
	@EJB
	CardManager ejbService;
	
	public List<Card> getCards( Integer id, String name, String description, Integer deckID, String pictureURL,
			  String type,String userName, Double rate, String rateOp, Integer rarity, String rarityOp) {
		
		
		List<QueryParameter> params = new ArrayList<QueryParameter>();
		try{
		
		
		if(id!=null)
		{
			params.add(new QueryParameter("id", QueryParamOperator.EQUALS, id));
		}
		if(name!=null)
		{
			params.add(new QueryParameter("name", QueryParamOperator.LIKE, name));
		}
		if(description!=null)
		{
			params.add(new QueryParameter("description", QueryParamOperator.LIKE, description));
		}
		if(pictureURL!=null)
		{
			params.add(new QueryParameter("pictureURL", QueryParamOperator.LIKE, pictureURL));
		}
		if(type!=null)
		{
			params.add(new QueryParameter("type", QueryParamOperator.LIKE, type));
		}
		if(deckID!=null)
		{
			params.add(new QueryParameter("deckID", QueryParamOperator.EQUALS, deckID));
		}
		if(userName!=null)
		{
			params.add(new QueryParameter("author", QueryParamOperator.LIKE, userName));
		}
		if(rarity!=null )
		{
			
			params.add(new QueryParameter("rarity", RestInputConverter.queryParamOperatorConverter(rarityOp), rarity));
		}
		if(rate!=null)
		{
			
			params.add(new QueryParameter("rate", RestInputConverter.queryParamOperatorConverter(rateOp), rate));
		}
		}catch(DAOException e)
		{
			return null;
		}
		

 
		return ejbService.getCards(params); 
 
	}
	
	
	public String createCard( String name, String description, String pictureURL, int deckID,
			  String type, int rarity,String userName,String sessionID) {
		
		int resultID;
		
		try {
			resultID = ejbService.createCard(name, description, pictureURL, type,rarity, userName, sessionID);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage(); 
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage(); 
		}
 
		return "OK The new card ID:"+String.valueOf(resultID); 
 
	}
	
	
	public String addCardToDeck( int cardID, int deckID,String userName,String sessionID)
	{
		try {
			ejbService.addCardtoDeck(cardID, deckID, userName, sessionID);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage();
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage(); 
		}
		
		return "OK";
	
	}
	
	
	public String updateCard( int id, String name, String description, String pictureURL,
			  String type, int rate, int rarity,String userName,String sessionID) {
		
		
		try {
			ejbService.updateCard(id,name, description, pictureURL, type,rate,rarity, userName, sessionID);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage(); 
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage(); 
		}

		return "OK"; 

	}
	
	public String deleteCard( int id,String userName,String sessionID) {
		try {
			ejbService.deleteCard(id, userName, sessionID);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage(); 
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage(); 
		}

		return "OK"; 
	}
	

	public String removeCardFromDeck( int cardID, int deckID,String userName,String sessionID)
	{
		try {
			ejbService.removeCardfromDeck(cardID, deckID, userName, sessionID);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage();
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage(); 
		}
		
		return "OK";
	
	}
	
	
	
}
